package net.huashitong.scheduling.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import net.huashitong.scheduling.entity.TownProjectDetail;
import net.huashitong.scheduling.entity.TownProjectList;
import net.huashitong.scheduling.filter.ProjectReserveFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class TownProjectDetailDao extends StringDao<TownProjectDetail> {

    public List<TownProjectDetail> queryByIdAndMonthAndYear(String studentId, ProjectReserveFilter filter){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.studentId = :studentId").setParam("studentId",studentId);
        query.append(" and obj.month = :month").setParam("month",filter.getMonth());
        query.append(" and obj.year = :year").setParam("year",filter.getYear());

        return query.listResult();
    }

    public List<Map<String, Object>> querytownDetailListByMonthAndYear(String studentId, ProjectReserveFilter filter){
        String hql=" select town_detail as townDetail,count(*) as townCount from t_town_project_detail where student_id ="+studentId+" and month = "+filter.getMonth()+" and year = "+filter.getYear()+" group by townDetail having townCount>=1 ORDER BY townCount DESC ";
        SQLQuery query = getSession().createSQLQuery(hql);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String, Object>> list=query.list();
        return list;
    }

    public List<TownProjectDetail>  queryByTownDetailAndMonthAndYear(String townDetail, ProjectReserveFilter filter ){
        GenericQuery query = createQuery("obj");
        System.out.print("studentId="+filter.getParentId());
        query.append(" and obj.studentId = :studentId").setParam("studentId",filter.getStudentId());
        query.append(" and obj.townDetail = :townDetail").setParam("townDetail",townDetail);
        query.append(" and obj.month = :month").setParam("month",filter.getMonth());
        query.append(" and obj.year = :year").setParam("year",filter.getYear());
        return query.listResult();
    }

}
